package com.xps.charmingshop.view.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.xps.charmingshop.App;
import com.xps.charmingshop.R;
import com.xps.charmingshop.adapter.CompleteAdapter;
import com.xps.charmingshop.adapter.ConductAdapter;
import com.xps.charmingshop.bean.CompleteBean;
import com.xps.charmingshop.bean.ConductBean;
import com.xps.charmingshop.date.net.UserNetWorks;
import com.xps.charmingshop.utils.ListDividerItemDecoration;
import com.xps.charmingshop.utils.SharedPreferencesUtils;
import com.xps.charmingshop.view.activity.DetailsActivity;
import com.xps.charmingshop.view.activity.OrderDetailsActivity;
import com.xps.charmingshop.view.base.BaseFragment;
import com.xps.charmingshop.view.base.BaseSwipeRefreshFragment;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import rx.Subscriber;


/**
 * Created by MI on 2018/1/10.
 */

public class CompleteFragment extends BaseFragment {
    //startActivity(new Intent(getActivity(), DetailsActivity.class));

    @BindView(R.id.Layout_Conduct)
    LinearLayout LayoutConduct;
    Unbinder unbinder;
    TextView Complete_ID;
    TextView Complete_Money;
    TextView Complete_Date;
    TextView Complete_State;
    TextView Complete_Order;

    @Override
    protected int getRes() {
        return R.layout.fragment_comlete;
    }

    @Override
    protected void init(View view) {
        unbinder = ButterKnife.bind(this, view);
    }

    @Override
    protected void setData(View view) {

    }

    @Override
    public void onClick(View v) {

    }

    @Override
    public void onResume() {
        super.onResume();
        doMore();
    }

   public void doMore() {
        String user_token = SharedPreferencesUtils.getString("user_token", "String", null);
        UserNetWorks.getComplete(user_token,"-1", new Subscriber<CompleteBean>() {

            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {
                e.printStackTrace();
            }

            @Override
            public void onNext(CompleteBean completeBean) {
                LayoutConduct.removeAllViews();
                List<CompleteBean.ReturnBodyBean> list = completeBean.getReturn_body();
                if (list != null && list.size() > 0) {
                    for (int i = 0; i < list.size(); i++) {
                        addOrderItem(list.get(i), LayoutConduct);
                        addDividerLine(LayoutConduct);
                    }
                }

            }
        });
    }

    /**
     * 添加分割线
     */
    public void addDividerLine(LinearLayout ll_lv) {
        View v = new View(getActivity());
        v.setAlpha(0.5f);
        v.setBackgroundColor(getActivity().getResources().getColor(R.color.dialogplus_card_shadow));
        v.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 1));
        ll_lv.addView(v);
    }

    /**
     * 添加订单
     * addOrderItem(DingDanBean.ReturnBodyBean dataBean)
     */
    public void addOrderItem(final CompleteBean.ReturnBodyBean dataBean, LinearLayout ll_lv) {
        View view = LayoutInflater.from(getActivity()).inflate(R.layout.adapter_complete, null);
        Complete_ID = view.findViewById(R.id.Complete_ID);
        Complete_Money = view.findViewById(R.id.Complete_Money);
        Complete_Date = view.findViewById(R.id.Complete_Date);
        Complete_State = view.findViewById(R.id.Complete_State);
        Complete_Order = view.findViewById(R.id.Complete_Order);
        Complete_ID.setText(dataBean.getOrder_id());
        parseTime(dataBean.getOrder_create_time(), Complete_Date);
        dateTime(dataBean.getOrder_create_time(), Complete_Money);
        if (dataBean.getOrder_status().equals("-1")) {
            Complete_State.setText("未处理");
            Complete_State.setTextColor(getResources().getColor(R.color.state_textblue));
        } else if (dataBean.getOrder_status().equals("1")) {
            Complete_State.setText("准备");
            Complete_State.setTextColor(getResources().getColor(R.color.orderlist_top));
        } else if (dataBean.getOrder_status().equals("2")) {
            Complete_State.setText("配送中");
            Complete_State.setTextColor(getResources().getColor(R.color.colorAccent));
        } else if (dataBean.getOrder_status().equals("3")) {
            Complete_State.setText("完成");
        } else if (dataBean.getOrder_status().equals("4")) {
            Complete_State.setText("取消");

        }
        Complete_Order.setText("查看");
        Complete_Order.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), OrderDetailsActivity.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                intent.putExtra("getOrder_id", dataBean.getOrder_id());
                intent.putExtra("order_state",dataBean.getOrder_status());
                getActivity().startActivity(intent);
            }
        });
        ll_lv.addView(view);
    }

    public void hide() {
        FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
        fragmentTransaction.addToBackStack(null);
        fragmentTransaction.hide(this);
        fragmentTransaction.commit();
    }

    /**
     * 解析时间
     */
    void parseTime(String createTime, TextView tvOrderTime) {
        long lTime = Long.valueOf(createTime);
        long nTime = lTime * 1000;
        Date passDate = new Date(nTime);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm");
        tvOrderTime.setText(simpleDateFormat.format(passDate));
    }

    /**
     * 解析时间
     */
    void dateTime(String createTime, TextView tvOrderTime) {
        long lTime = Long.valueOf(createTime);
        long nTime = lTime * 1000;
        Date passDate = new Date(nTime);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        tvOrderTime.setText(simpleDateFormat.format(passDate));
    }

}